package paystation.monitor;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

import paystation.domain.StatusListener;
import paystation.domain.StatusObservable;

/**
 * A crude graphical monitor application.
 */
public class StatusFrame extends JFrame {
    /**
	 * 
	 */
	private static final long serialVersionUID = -3738899871627290575L;
	private JLabel vLabel, eLabel;
    private StatusListener myListener;

    public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // ignore silently, as we then just do not get the required
            // look and feel for all windows
        }
        StatusFrame clientMonitor = new StatusFrame(580,10);
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		if (args.length > 0) {
			String host = args[0];
		}

		System.out.println("Lookup PayStationServer");
		clientMonitor.attachStatusListener();
    }

    public StatusFrame(int x, int y) throws RemoteException {
        super("Supervisor");
        setLocation(x, y);
        vLabel = new JLabel("  Total Earning xxxx  ");
        eLabel = new JLabel("  Number of vacant: 200 ");
        Container pane = getContentPane();
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        pane.add(vLabel);
        pane.add(eLabel);

        myListener = new LabelUpdater(vLabel, eLabel);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        pack();
        setVisible(true);
    }
    
    public void attachStatusListener() throws MalformedURLException, RemoteException, NotBoundException
    {
		String host = "localhost";
		
		//Torben test
		
		final StatusListener proxy = (StatusListener) UnicastRemoteObject.exportObject(myListener, 0);
		
		String[] names = Naming.list("//" + host + "/");
		for (int i = 0; i < names.length; i++) {
			System.out.println("Naming.List " + i + " " + names[i]);
		
			//StatusObservable observablePayStation = (StatusObservable) Naming.lookup("//" + host + "/PayStation");	
			StatusObservable observablePayStation = (StatusObservable) Naming.lookup(names[i]);	
		
        	observablePayStation.addStatusListener(proxy);
		}
    }
    
    public StatusListener getStatusListener() {
        return myListener;
    }
}
